// pages/goodinfo/GoodInfoMethodItem/index.js
Component({
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    goodInfo: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    result: [],
    childResult: [],
    normIndex: 0,
    countPrice: 0,
    sampleNumber: '',
    renderMethods: [],
    isRender: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    repeatShowhandle() {
      this.triggerEvent('repeatShowhandle')
    },
    onChange(event) {
      const currentIsSelected = event.detail.includes(
        `${this.data.goodInfo.normList[this.data.normIndex].id}`
      )
      let childResult
      // 全选
      if (currentIsSelected) {
        childResult = [
          ...new Set([
            ...this.data.goodInfo.normList[this.data.normIndex].methodVOList.map(
              (item) => `${item.id}`
            ),
            ...this.data.childResult
          ])
        ]
      } else {
        // 取消全选
        const list = this.data.goodInfo.normList[this.data.normIndex].methodVOList.map(
          (item) => `${item.id}`
        )
        console.log(list, '=====')
        childResult = JSON.parse(JSON.stringify(this.data.childResult))
        console.log(childResult, '----')
        list.forEach((el) => {
          const index = childResult.findIndex((item) => item === el)
          index > -1 && childResult.splice(index, 1)
        })
      }

      this.setData(
        {
          result: event.detail,
          childResult
        },
        () => {
          this.computedPrice()
        }
      )
    },

    onChildChange(event) {
      // let result = []
      // if (event.detail.length > 0) {
      //   const normList = this.data.goodInfo.normList
      //   for (let i = 0; i < normList.length; i++) {
      //     const norm = normList[i]
      //     const methodVOList = norm.methodVOList
      //     for (let j = 0; j < methodVOList.length; j++) {
      //       const methodVO = methodVOList[j]
      //       const target = event.detail.includes(`${methodVO.id}`)
      //       if (target) {
      //         result.push(`${norm.id}`)
      //       }
      //     }
      //   }
      //   result = [...new Set(result)]
      // }

      this.setData(
        {
          childResult: event.detail
          // result
        },
        () => {
          this.computedPrice()
        }
      )
    },

    onsearch(e) {
      const keyWords = e.detail
      this.getRenderMethods(keyWords)
    },

    getRenderMethods(keyWords) {
      const renderMethods = this.data.goodInfo.normList.filter((norm) => {
        return norm.testNormName.includes(keyWords)
      })
      this.setData(
        {
          isRender: false
        },
        () => {
          setTimeout(() => {
            this.setData({
              renderMethods,
              isRender: true
            })
          }, 0)
        }
      )
    },

    toggle(event) {
      const { index } = event.currentTarget.dataset
      const checkbox = this.selectComponent(`.checkboxes-${index}`)
      this.setData(
        {
          normIndex: index
        },
        () => {
          checkbox.toggle()
        }
      )
    },

    noop(e) {
      this.setData({
        normIndex: e.currentTarget.dataset.index
      })
    },

    childNoop(e) {
      this.setData({
        normIndex: e.currentTarget.dataset.index
      })
    },

    computedPrice() {
      let price = 0
      const normList = this.data.goodInfo.normList
      for (let i = 0; i < normList.length; i++) {
        const norm = normList[i]
        const methodVOList = norm.methodVOList
        for (let j = 0; j < methodVOList.length; j++) {
          const methodVO = methodVOList[j]
          const target = this.data.childResult.includes(`${methodVO.id}`)
          if (target) {
            price += Number(methodVO.price) * 100
          }
        }
      }
      this.setData(
        {
          countPrice: price
        },
        () => {
          this.triggerEvent('computedPrice')
        }
      )
    }
  },
  lifetimes: {
    attached() {
      this.setData({
        renderMethods: this.data.goodInfo.normList
      })
    }
  }
})
